package modes

import (
	"fmt"
)

type SetUserClass struct {
	Class    `json:"cla" xorm:"extends"`
	SubClass `json:"sub" xorm:"extends"`
}

func (this *SetUserClass) List(val *Pager) error {
	list := make([]SetBlem, 0)
	val.TName = "user_class"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 状态
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(0).Table(val.TName).
		Join("LEFT", "c_subject", fmt.Sprintf("%s.cid = c_subject.c_id", val.TName)).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}
